package com.itheima.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.api.SmsFlashPromotionApi;
import com.itheima.dto.CreateCouponDto;
import com.itheima.exception.ConsumerException;
import com.itheima.pojo.SmsFlashPromotion;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Hakuna Matata
 * @date 2022-10-12-17:44
 */
@Service
public class FlashPromotionService {
    @DubboReference
    private SmsFlashPromotionApi smsFlashPromotionApi;

    //添加活动
    public void flashPromotionCreate(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionApi.flashPromotionCreate(smsFlashPromotion);
    }

    //删除活动
    public void flashPromotionDelete(Long id) {
        smsFlashPromotionApi.flashPromotionDelete(id);
    }

    //根据活动名称分页查询
    public PageResult getPromotionByPage(String keyword, Integer pageNum, Integer pageSize) {

        return smsFlashPromotionApi.getPromotionByPage(keyword, pageNum, pageSize);
    }

    //修改上下线状态
    public void updateStatus(Long id, Integer status) {
        SmsFlashPromotion promotion = smsFlashPromotionApi.findById(id);
        if (ObjectUtil.isNotNull(promotion)) {
            smsFlashPromotionApi.updateStatus(id, status);
        } else {
            //如果活动不存在
            throw new ConsumerException("该活动不存在");
        }
    }

    //编辑活动
    public void updatePromotion(Long id, SmsFlashPromotion flashPromotion) {
        //根据id找到flashPromotion
        //SmsFlashPromotion promotion = smsFlashPromotionApi.findById(id);
        System.err.println(flashPromotion.getId());
        smsFlashPromotionApi.updatePromotion(flashPromotion);
    }

    //根据id找到flashPromotion
    public SmsFlashPromotion findPromotionById(Long id) {
        return smsFlashPromotionApi.findById(id);
    }
}
